﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _42_Kicks
{
    public enum Type
    {
        Chauve, Roux
    }
    public abstract class Ennemi : AniSprite
    {

        int KOtimer = 9;
        public Rectangle collisionbis;
        public int chrono = 0;
        //Pour un ennemi ce vecteur servira, si l'ennemi peut sauter, à le dessiner en saut, sinon à gérer les collisions avec le héros


        public int t1 = 0;
        //Pour les collisions
        public int decalageX;
        public int decalageY;
        public int largeur;
        public int hauteur;

        //decalageX2 et decalageY2 servent à definir la hitbox lors des sauts
        public int decalageX2;
        public int decalageY2;
        public int largeur2;
        public int hauteur2;

        public int pointdevie;
        //Ces booléens servirons non pas à déclencher les fonctions de marche, saut... mais des effets lors de collisions, des coups ce genre de choses
        public bool marche;
        public bool saute;
        public bool pirouette;
        public bool deplat = true;
        public bool frappe = false;
        public bool estfrappe = false;
        public bool KO = false;

        public bool Particule = false;

        //Pour le son



        //Pour la projection

        public int projectiontimer = 450;
        public bool projeté = false;
        public int chutetimer = 2000;
        public bool sereleve = false;
        public bool Bruitdecalotte = false;




        //Constructeurs de la classe Ennemi
        public Ennemi(Vector2 position, Rectangle source)
            : base(position, source)
        { }
        public Ennemi(float posx, float posy, int x, int y, int width, int height)
            : base(posx, posy, x, y, width, height)
        { }
        public Ennemi(Texture2D texture)
            : base(texture)
        { }

        public override void LoadContent(ContentManager content, string assetName)
        {
            texture = content.Load<Texture2D>(assetName);

        }
        public virtual bool Collision(Rectangle colli)
        {

            Rectangle Pos = new Rectangle((int)position.X + decalageX, (int)position.Y + decalageY,
                                    largeur, hauteur);

            return Pos.Intersects(colli);

        }

        public bool Drop(GameTime gameTime)
        {
            Random rand;
            rand = new Random();
            if (KO && KOtimer >= 0)
            {
                KOtimer -= gameTime.ElapsedGameTime.Milliseconds;
                int Nbrandom = rand.Next(0, 6);
                if (Nbrandom == 4)
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }


    }
    // Fin de la classe ennemi

    public class Chauve : Ennemi
    {


        //détermine le type d'ennemi
        protected Type type;




        public Chauve(float posx, float posy, int x, int y, int width, int height, Type type)
            : base(posx, posy, x, y, width, height)
        { collisionbis = new Rectangle(Convert.ToInt32(posx), Convert.ToInt32(posy), Convert.ToInt32(width), Convert.ToInt32(height)); if (type == Type.Chauve) { pointdevie = 250; } if (type == Type.Roux) { pointdevie = 375; } this.type = type; }


        public override void Update(Vector2 translation)
        {
            if (!KO)
            {
                if (marche || !frappe && !marche)

                { decalageX = 65; largeur = 22; decalageY = 125; hauteur = 42; hauteur2 = 137; decalageY2 = 14; }
                if (frappe && !marche)
                { decalageX = 65; largeur = 22; decalageY = 125; hauteur = 42; hauteur2 = 137; decalageY2 = 14; }



                position += translation;
                collisionbis.X += (Convert.ToInt32(translation.X) + decalageX);
                collisionbis.X += (Convert.ToInt32(translation.Y) + decalageY2);
                collisionbis.Height = hauteur2;
                collisionbis.Width = largeur;
            }
        }

        public override void LoadContent(ContentManager content, string assetName)
        {
            if (type == Type.Roux)
            { base.LoadContent(content, "Roux"); }
            if (type == Type.Chauve)
            { base.LoadContent(content, "Chauve"); }

        }

        public virtual void Déplacement(Heros heros1, Heros heros2, GameTime gameTime)
        {
            #region target
            //pour obtenir le joueur le plus proche
            Heros target = null;
            if (!heros1.KO && heros2.KO)
            {
                target = heros1;
            }
            else if (!heros2.KO && heros1.KO)
            {
                target = heros2;
            }
            else if ((this.position.X - heros1.position.X) * (this.position.X - heros1.position.X) >= (this.position.X - heros2.position.X) * (this.position.X - heros2.position.X))
            {
                target = heros2;
            }
            else
            {
                target = heros1;
            }
            #endregion target

            //gestion du deplacement
            if (!KO && !projeté && chutetimer == 2000)
            {
                if (!Collision(target.position2))
                {

                    if (target.position.X > position.X)
                    {
                        marche = true;
                        Update(new Vector2(1 * gameTime.ElapsedGameTime.Milliseconds * 0.20f, 0));

                    }
                    if (target.position.X < position.X)
                    {
                        marche = true;
                        Update(new Vector2(-1 * gameTime.ElapsedGameTime.Milliseconds * 0.20f, 0));

                    }

                    if (target.position.Y > position.Y)
                    {
                        if (target.position.X < position.X)
                        {
                            marche = true;
                            Update(new Vector2(0, 1 * gameTime.ElapsedGameTime.Milliseconds * 0.20f));
                            DefileSpriteGauche(0, 453, 303, 151, 151, gameTime);
                        }
                        else
                        {
                            marche = true;
                            Update(new Vector2(0, 1 * gameTime.ElapsedGameTime.Milliseconds * 0.20f));
                            DefileSprite(0, 604, 0, 151, 151, gameTime);
                        }
                    }

                    if (target.position.Y < position.Y)
                    {

                        if (target.position.X < position.X)
                        {
                            marche = true;
                            Update(new Vector2(0, -1 * gameTime.ElapsedGameTime.Milliseconds * 0.20f));
                            //DefileSpriteGauche(0, 453, 303, 151, 151, gameTime);
                        }
                        else
                        {
                            marche = true;
                            Update(new Vector2(0, -1 * gameTime.ElapsedGameTime.Milliseconds * 0.20f));
                            DefileSprite(0, 604, 0, 151, 151, gameTime);
                        }
                    }
                    else marche = false;
                }
            }

        }

        public void perd_PV(player1 heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup == Coups.Kick && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch1 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.Punch2 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.KickCombo1 && heros.t2 < 110 && heros.t2 > 100)
                {
                    if ((position.X >= heros.position.X && heros.Frappedroite) || (position.X <= heros.position.X && heros.Frappegauche))
                    {
                        Particule = true;
                        if (!heros.DmG)
                        {
                            pointdevie -= 25;
                        }
                        if (heros.DmG)
                        {
                            pointdevie -= 100;
                        }
                    }
                }
                if (Collision(heros.position2) && heros.coup == Coups.KickCombo2 && heros.t2 < 255 && heros.t2 > 240)
                {
                    if ((position.X >= heros.position.X && heros.Frappedroite) || (position.X <= heros.position.X && heros.Frappegauche))
                    {
                        Particule = true;
                        if (!heros.DmG)
                        {
                            pointdevie -= 50;
                        }
                        if (heros.DmG)
                        {
                            pointdevie -= 200;
                        }
                    }
                }
                if (pointdevie <= 0)
                { KO = true; }
                if (KO)
                {

                    chrono -= gameTime.ElapsedGameTime.Milliseconds;
                    if (pointdevie <= 0 && chrono > 0)
                    {

                        DefileSprite(0, 906, 604, 175, 151, gameTime);
                    }

                }

            }

        }
        public void perd_PV(player2 heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup == Coups.Kick && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch1 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.Punch2 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.KickCombo1 && heros.t2 < 110 && heros.t2 > 100)
                    if ((position.X >= heros.position.X && heros.Frappedroite) || (position.X <= heros.position.X && heros.Frappegauche))
                    {
                        Particule = true;
                        if (!heros.DmG)
                        {
                            pointdevie -= 25;
                        }
                        if (heros.DmG)
                        {
                            pointdevie -= 100;
                        }
                    }
                if (Collision(heros.position2) && heros.coup == Coups.KickCombo2 && heros.t2 < 255 && heros.t2 > 240)
                {
                    Particule = true;
                    if (!heros.DmG)
                    {
                        pointdevie -= 50;
                    }
                    if (heros.DmG)
                    {
                        pointdevie -= 200;
                    }
                }
                if (pointdevie <= 0)
                { KO = true; }
                if (KO)
                {

                    chrono -= gameTime.ElapsedGameTime.Milliseconds;
                    if (pointdevie <= 0 && chrono > 0)
                    {

                        DefileSprite(0, 906, 604, 175, 151, gameTime);
                    }

                }

            }

        }

        public void BRUITAGES(Heros heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup != Coups.Rien && heros.t2 < 110 && heros.t2 > 85)
                {
                    Bruitdecalotte = true;
                }
                else Bruitdecalotte = false;
            }
        }

        public void projection(Heros heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup == Coups.KickCombo2 && heros.t2 < 255 && heros.t2 > 240)
                    if ((position.X >= heros.position.X && heros.Frappedroite && !projeté) || (position.X <= heros.position.X && heros.Frappegauche && !projeté))
                    {
                        projeté = true;

                    }
                if (projeté)
                {
                    projectiontimer -= gameTime.ElapsedGameTime.Milliseconds;
                    chutetimer -= gameTime.ElapsedGameTime.Milliseconds;
                }
                if (projeté && projectiontimer > 0 && !sereleve)
                {
                    if (position.X >= heros.position.X && projectiontimer >= 0)
                    {
                        position.X += 3;
                        DefileSpriteGauche(0, 1661, 755, 100, 151, gameTime);

                    }
                    if (position.X < heros.position.X)
                    {
                        position.X -= 3;
                        DefileSprite(0, 1661, 604, 100, 151, gameTime);
                    }

                }
                if (projeté && chutetimer > 0 && projectiontimer <= 0)
                {
                    sereleve = true;
                }
                if (projeté && chutetimer > 0)
                {

                    if (position.X >= heros.position.X && projectiontimer >= 0)
                    {
                        DefileSpriteGauche(0, 1661, 755, 100, 151, gameTime);

                    }
                    if (position.X < heros.position.X)
                    {
                        DefileSprite(0, 1661, 604, 100, 151, gameTime);
                    }
                }
                if (projectiontimer <= 0)
                {

                    projectiontimer = 450;

                }
                if (chutetimer <= 0)
                {
                    projeté = false;
                    chutetimer = 2000;
                    sereleve = false;
                }
            }
        }

        public void Attaque(Heros heros, GameTime gameTime)
        {
            if (Collision(heros.position2) && !frappe && chrono <= 0)
            {
                frappe = true;
                chrono = 707; //(357 temps de l'animation + 350 temps d'inaction pour laisser le temps au joueur de souffler un peu)
            }
            else if (chrono >= 0)
            {
                chrono -= gameTime.ElapsedGameTime.Milliseconds;
            }

            if (frappe && Collision(heros.position2) && heros.position.X < position.X && !heros.Frappedroite || frappe && Collision(heros.position2) && heros.position.X >= position.X && !heros.Frappegauche)
            {
                if (!heros.saute)
                {

                    if (heros.position.X > position.X)
                    {

                        DefileSprite(0, 453, 151, 122, 151, gameTime);

                    }
                    if (heros.position.X < position.X)
                    {

                        DefileSpriteGauche(0, 453, 453, 122, 151, gameTime);
                    }

                    if (!heros.GodMode && !KO && chrono <= 707 && chrono >= 700)//if (chrono <= 347 && chrono >= 340), cas sans temps d'inaction de l'ennemi
                    {
                        heros.pointdevie -= 10;


                    }
                    if (!heros.estfrappedroite && !heros.estfrappegauche && heros.timerestfrappe == 300)
                    {
                        if (heros.source.Y == 0 || heros.source.Y == 302 || heros.source.Y == 604 || heros.source.Y == 906)
                        {
                            heros.estfrappedroite = true;
                        }
                        if (heros.source.Y == 151 || heros.source.Y == 453 || heros.source.Y == 755 || heros.source.Y == 1057)
                        {
                            heros.estfrappegauche = true;
                        }
                    }
                    if (chrono < 350)
                    {
                        frappe = false;

                    }
                }
            }
        }



    }

    public class Boss : Ennemi
    {
        public bool attaque = false;
        public bool retour = false;
        public bool pret = false;
        public bool monte = true;

        public Boss(float posx, float posy, int x, int y, int width, int height)
            : base(posx, posy, x, y, width, height)
        { collisionbis = new Rectangle(Convert.ToInt32(posx), Convert.ToInt32(posy), Convert.ToInt32(width), Convert.ToInt32(height)); pointdevie = 500; }

        public override void Update(Vector2 translation)
        {
            if (!KO)
            {
                if (marche || !frappe && !marche)

                { decalageX = 65; largeur = 22; decalageY = 125; hauteur = 42; hauteur2 = 137; decalageY2 = 14; }
                if (frappe && !marche)
                { decalageX = 65; largeur = 22; decalageY = 125; hauteur = 42; hauteur2 = 137; decalageY2 = 14; }



                position += translation;
                collisionbis.X += (Convert.ToInt32(translation.X) + decalageX);
                collisionbis.X += (Convert.ToInt32(translation.Y) + decalageY2);
                collisionbis.Height = hauteur2;
                collisionbis.Width = largeur;
            }
        }

        public override void LoadContent(ContentManager content, string assetName)
        {
            base.LoadContent(content, "Boss");
        }

        public virtual void Déplacement(Heros heros1, Heros heros2, GameTime gameTime)
        {

            //gestion du deplacement
            if (!KO)
            {
                marche = true;
                if (position.X >= 800)
                {
                    pret = true;
                    retour = false;
                }

                if (pret && !attaque)
                {
                    Random rand = new Random();
                    if (rand.Next(0, 10000) >= 9900)
                    {
                        attaque = true;
                    }
                    else
                    {
                        float Max;
                        float Min;
                        if (heros1.position.Y <= heros2.position.Y)
                        {
                            Max = heros1.position.Y;
                            Min = heros2.position.Y;

                        }
                        else
                        {
                            Max = heros2.position.Y;
                            Min = heros1.position.Y;
                        }

                        if (monte && !retour)
                        {
                            if (position.Y >= Max)
                            {
                                Update(new Vector2(0, -1 * gameTime.ElapsedGameTime.Milliseconds * 0.15f));
                                DefileSpriteGauche(1208 + 151, 1680, 151, 151, 151, gameTime);
                            }
                            else
                            {
                                monte = false;
                            }
                        }
                        if (!monte && !retour)
                        {
                            if (position.Y <= Min)
                            {
                                Update(new Vector2(0, 1 * gameTime.ElapsedGameTime.Milliseconds * 0.15f));
                                DefileSpriteGauche(1208 + 151, 1680, 151, 151, 151, gameTime);
                            }
                            else
                            {
                                monte = true;
                            }
                        }
                    }
                }
                if (attaque)
                {
                    Update(new Vector2(-1 * gameTime.ElapsedGameTime.Milliseconds * 0.45f, 0));
                    DefileSpriteGauche(0, 604, 1057, 110, 151, gameTime);
                }
                if (retour)
                {
                    Update(new Vector2(1 * gameTime.ElapsedGameTime.Milliseconds * 0.15f, 0));
                    DefileSprite(0, 604, 0, 151, 151, gameTime);
                }
            }
        }

        public void perd_PV(player1 heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup == Coups.Kick && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch1 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.Punch2 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.KickCombo1 && heros.t2 < 110 && heros.t2 > 100)
                {
                    if ((position.X >= heros.position.X && heros.Frappedroite) || (position.X <= heros.position.X && heros.Frappegauche))
                    {
                        Particule = true;
                        if (!heros.DmG)
                        {
                            pointdevie -= 25;
                        }
                        if (heros.DmG)
                        {
                            pointdevie -= 250;
                        }
                    }
                }
                if (Collision(heros.position2) && heros.coup == Coups.KickCombo2 && heros.t2 < 255 && heros.t2 > 240)
                {
                    if ((position.X >= heros.position.X && heros.Frappedroite) || (position.X <= heros.position.X && heros.Frappegauche))
                    {
                        Particule = true;
                        if (!heros.DmG)
                        {
                            pointdevie -= 50;
                        }
                        if (heros.DmG)
                        {
                            pointdevie -= 250;
                        }
                    }
                }
                if (pointdevie <= 0)
                { KO = true; }
                if (KO)
                {

                    chrono -= gameTime.ElapsedGameTime.Milliseconds;
                    if (pointdevie <= 0 && chrono > 0)
                    {
                        DefileSprite(0, 906, 604, 175, 151, gameTime);
                    }

                }

            }

        }
        public void perd_PV(player2 heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup == Coups.Kick && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch && heros.t2 < 255 && heros.t2 > 240 || Collision(heros.position2) && heros.coup == Coups.Punch1 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.Punch2 && heros.t2 < 110 && heros.t2 > 100 || Collision(heros.position2) && heros.coup == Coups.KickCombo1 && heros.t2 < 110 && heros.t2 > 100)
                    if ((position.X >= heros.position.X && heros.Frappedroite) || (position.X <= heros.position.X && heros.Frappegauche))
                    {
                        Particule = true;
                        if (!heros.DmG)
                        {
                            pointdevie -= 25;
                        }
                        if (heros.DmG)
                        {
                            pointdevie -= 100;
                        }
                    }
                if (Collision(heros.position2) && heros.coup == Coups.KickCombo2 && heros.t2 < 255 && heros.t2 > 240)
                {
                    Particule = true;
                    if (!heros.DmG)
                    {
                        pointdevie -= 50;
                    }
                    if (heros.DmG)
                    {
                        pointdevie -= 200;
                    }
                }
                if (pointdevie <= 0)
                { KO = true; }
                if (KO)
                {

                    chrono -= gameTime.ElapsedGameTime.Milliseconds;
                    if (pointdevie <= 0 && chrono > 0)
                    {

                        DefileSprite(0, 906, 604, 175, 151, gameTime);
                    }

                }

            }

        }

        public void BRUITAGES(Heros heros, GameTime gameTime)
        {
            if (!KO)
            {
                if (Collision(heros.position2) && heros.coup != Coups.Rien && heros.t2 < 325 && heros.t2 > 300)
                {
                    Bruitdecalotte = true;
                }
                else Bruitdecalotte = false;
            }
        }

        public void Attaque(Heros heros, GameTime gameTime)
        {
            if (Collision(heros.position2) && !heros.KO)
            {
                if (pret)
                {
                    pret = false;
                    attaque = true;
                    retour = false;
                }
                else
                {
                    attaque = false;
                    retour = true;
                }
            }

            if ((attaque && position.X >= 151))
            {
                if (Collision(heros.position2) && !retour && !heros.GodMode && !heros.KO && !heros.saute)
                {
                    Bruitdecalotte = true;
                    heros.pointdevie -= 50;
                    heros.projeté = true;
                    attaque = false;
                    retour = true;
                }
            }
            else
            {
                attaque = false;
                retour = true;
            }

        }
    }

}


